api = APP.api

View = ->
    exports = {}
    $view = $(AT.Home())
    APP.showNavMenu()

    appSize = 40
    appCount = 32;
    appPageNo = 1;
    appPageCount = 1;

    $('body').append $view

    $pagination = $(".pagination", $view)

    $modelContent = $('.modal-content', $view)
    $appBoard = $('.app-board', $modelContent)
    appPageCount = Math.ceil(appSize / appCount)
    $ul = $("<ul>").appendTo $appBoard


    for i in [1..appSize]
        if i > appCount
            currentPage = Math.ceil(appSize / appCount)
            if !$(".app-board#{currentPage}", $view).length
                $appBoard = $("<div>", {
                    class: "app-board app-board#{currentPage}",
                    style: "left:#{currentPage - 1}00%"
                }).appendTo $modelContent
                $ul = $("<ul>").appendTo $appBoard
        $app = $("<li>", class: "app").appendTo $ul
        $appContent = $("<div>", class: "app-content").appendTo $app
        $link = $("<a>", href: "weixin://data/").appendTo $appContent
        $icon = $("<img>", {class: "app-icon", src: "/img/app/#{i}.ico"}).appendTo $link
        $name = $("<div>", {class: "app-name", text: "#{i}"}).appendTo $link

    # 触发model
    $('.modal-trigger').leanModal()

    changePageStats = ->
        $icons = $pagination.find(".icon")
        $icons.removeClass("active").eq(appPageNo - 1).addClass("active")

    $view.on 'click', '.app .app-icon', ->
        $this = $ this
        $this.addClass("active z-depth-1")
        setTimeout((-> $this.removeClass("active z-depth-1")), 300)


    hammertime = new Hammer($modelContent[0], {threshold: 0, pointers: 0})
    hammertime.on 'panend', (ev) ->
        if ev.deltaX / $(".app-board")[0].clientWidth < -0.2 && appPageNo < appPageCount
# 右移
            appPageNo++;
            changePageStats();
            move('.modal-content')
            .ease('in-out').x(-$(".app-board").width())
            .duration('.2s')
            .end()
        else if ev.deltaX / $(".app-board")[0].clientWidth > 0.2 && appPageNo > 1
# 右移
            appPageNo--;
            changePageStats();
            move('.modal-content')
            .ease('in-out').x(-$(".app-board").width() * (appPageNo - 1))
            .duration('.2s')
            .end()
        else
            move('.modal-content')
            .ease('in-out').x(-$(".app-board").width() * (appPageNo - 1))
            .duration('.1s')
            .end()


    hammertime.on 'pan', (ev) ->
        return if (ev.deltaX % $(".app-board")[0].clientWidth) / $(".app-board")[0].clientWidth < -0.4 && appPageNo == appPageCount
        if ev.deltaX < 0 && appPageNo == appPageCount
            move('.modal-content')
            .x(-$(".app-board").width() * (appPageCount - 1) + (ev.deltaX * Math.cos((-ev.deltaX / $(".app-board")[0].clientWidth) * Math.PI / 180)) / 4)
            .duration('0')
            .end()
        else if ev.deltaX > 0 && appPageNo == 1
            move('.modal-content')
            .x((ev.deltaX * Math.cos((ev.deltaX / $(".app-board")[0].clientWidth) * Math.PI / 180)) / 4)
            .duration('0')
            .end()
        else
            move('.modal-content')
            .translateX(-$(".app-board").width() * (appPageNo - 1) + ev.deltaX)
            .duration('0s')
            .end()


    exports.$view = $view

    return exports


APP.app.toHome = ->
    view = APP.getView 'home', View

    $('body > .view').hide()
    view.$view.show()